const app = new Vue({
  el: '#app',
  data: {
    message: '你好',
    books: [
      {
        id: 1,
        name: "《算法导论》",
        date: '2020-01-01',
        price: 80,
        number: 1
      },
      {
        id: 2,
        name: "《Java》",
        date: '2010-01-01',
        price: 30,
        number: 2
      },
      {
        id: 3,
        name: "《Vue》",
        date: '2030-01-01',
        price: 10,
        number: 3
      },
      {
        id: 4,
        name: "《html》",
        date: '2310-01-01',
        price: 1230,
        number: 4
      },
      {
        id: 5,
        name: "《js》",
        date: '2066-01-01',
        price: 1320,
        number: 5
      }
    ]
  },
  filters: {
    showPrice(price) {
      return "￥" + price.toFixed(2)
    }
  },
  computed: {
    totalPrice111() {
      let totalprice = 0
      for (let i = 0; i< this.books.length; i++){
        totalprice += this.books[i].price * this.books[i].number
      }
      return totalprice
    },
    totalPrice() {
      // return this.books.reduce(function (pre, book) {
      //   return pre + book.price * book.number
      // }, 0)
      return this.books.reduce((pre, book) => pre + book.price * book.number ,0)
    }
  },

  methods: {
    increment(index) {
      this.books[index].number++
    },
    decrement(index) {
      this.books[index].number--
    },
    removeHandler(index) {
      this.books.splice(index, 1)
    }
  }
})


// filter 过滤，回调函数返回结果： true 则加入到新的数组； false 则不加入到新的数组
// map 映射， 回调函数返回结果直接加入到新的数组
// reduce 汇总， 返回结果作为下次调用的pre参数

let list = [22,33,14,44,1222,1232,3124]

let total1= list.filter(n => n < 100).map(n => n * 2).reduce((pre, n) => pre + n ,0)

let total = list.filter(function (n) {
  return n < 100
}).map(function (n) {
  return n * 2
}).reduce(function (pre, n) {
  return n + pre
}, 0)

console.log(total)
console.log(total1)
